/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.model;

import com.wisnu.spms.service.dao.DbBillingDao;
import com.wisnu.spms.service.dao.DbBillingTypeDao;
import com.wisnu.spms.service.entity.DbBilling;
import com.wisnu.spms.service.entity.DbBillingtype;
import com.wisnu.spms.service.entity.DbStatus;
import com.wisnu.spms.service.entity.DbStudent;
import com.wisnu.spms.service.util.SpringUtil;
import com.wisnu.spms.source.listener.PanelBillingListener;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelBillingModel {

    private Integer semester;
    private DbStudent student;
    private DbBillingtype billingtype;
    private List<DbBilling> list = new ArrayList<>();
    private final PanelBillingListener listener;

    public PanelBillingModel(PanelBillingListener listener) {
        this.listener = listener;
    }

    public void setSemester(Integer semester) {
        this.semester = semester;
    }

    public Integer getSemester() {
        return semester;
    }

    public DbStudent getStudent() {
        return student;
    }

    public void setStudent(DbStudent student) {
        this.student = student;
    }

    public List<DbBilling> getList() {
        return list;
    }

    public void setList(List<DbBilling> list) {
        this.list = list;
        onChange(this);
    }

    public void setBillingtype(DbBillingtype billingtype) {
        this.billingtype = billingtype;
    }

    public DbBillingtype getBillingtype() {
        return billingtype;
    }

    public void getDbBillings() throws Exception {
        DbBillingDao billingDao = SpringUtil.getBillingDao();
        setList(billingDao.GetDbBillingBySemester(student, semester));
    }

    public List<DbBillingtype> getBillingtypes() throws Exception {
        DbBillingTypeDao billingTypeDao = SpringUtil.getBillingTypeDao();
        return billingTypeDao.getBillingtypes();
    }

    public void onChange(PanelBillingModel model) {
        listener.onChange(model);
    }

    public void insertBilling() throws Exception {
        DbBilling billing = new DbBilling();
        billing.setDbBillingtype(billingtype);
        billing.setDbStatus(new DbStatus(1, "Belum Lunas"));
        billing.setStudentSemester(semester);
        billing.setDbStudent(student);
        
        DbBillingDao billingDao = SpringUtil.getBillingDao();
        billingDao.insertBilling(billing);
    }

}
